package com.caishi.lkx.config;

import com.caishi.lkx.handler.PkWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private PkWebSocketHandler webSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler, "/websocket/pk")
                .setAllowedOrigins("*");
    }
    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        // 设置文本消息缓冲区大小 (默认8192字节)
        container.setMaxTextMessageBufferSize(1024 * 1024); // 1MB
        // 设置二进制消息缓冲区大小 (默认8192字节)
        container.setMaxBinaryMessageBufferSize(1024 * 1024); // 1MB

        // 设置最大会话空闲时间 (默认0，表示不限制)
//        container.setMaxSessionIdleTimeout(15 * 60000L); // 15分钟
        // 设置异步发送超时时间 (默认0，表示不限制)
//        container.setAsyncSendTimeout(10000L); // 10秒
        return container;
    }
}

